/*********************************************************
          File Name:9.52.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 09 Feb 2016 09:17:29 PM CST
**********************************************************/

#include "9.52.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <deque>

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        std::cout << argv[0] << " (xxx)\n";
        std::cout << "input something with parentheses!\n";
        exit(1);
    }

    //nm::stack<char, std::deque<char>> stk;
    nm::stack<char> stk;
    //nm::stack<char, std::vector<char>> stk;
    std::string s(argv[1]);
    std::ostringstream ss;
    std::vector<std::string> res;
    bool ok = false;

    for(auto &x: s)
    {
        if(x != ')')
            stk.push(x);
        if(x == ')')
        {
            ok = false;
            while(!stk.empty())
            {
                if(stk.top() == '(')
                {
                    stk.pop();
                    ok = true;
                    break;
                }
                ss << stk.top();
                stk.pop();
            }
            if(ss.str() != "")
            {
                res.push_back(ss.str());
                ss.str("");
            }
            if(stk.empty() && !ok)
            {
                std::cerr << __LINE__ << ": parentheses not match!\n";
                exit(1);
            }
        }
    }

    if(!stk.empty())
    {
        ss.str("");
        while(!stk.empty())
        {
            if(stk.top() == ')' || stk.top() == '(')
            {
                std::cerr << __LINE__ << ": parentheses not match!\n";
                exit(1);
            }
            ss << stk.top();
            stk.pop();
        }
        if(ss.str() != "")
            res.push_back(ss.str());
    }

    auto indent = res.size();
    size_t tmp;
    for(const auto &x: res)
    {
        tmp = indent;
        while(tmp != 0)
        {
            tmp -= 1;
            std::cout << " ";
        }
        std::cout << x << std::endl;
        indent -= 1;
    }

    return 0;
}
